/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ym.util.uuid;

import java.util.Random;

/**
 *
 * @author fyi
 */
public class RandomSelect {

    public static <T> void randomSelect(T[] ts, int n, Random r) {
        int m = ts.length;
        if (n < 0) {
            throw new IllegalArgumentException(String.format("n(%d)<0", n));
        }
        if (n >= m) {
            throw new IllegalArgumentException(String.format(
                    "n(%d)>=ts.length(%d)", n, m));
        }

        for (int i = 0; i < n; i++) {
            int a = r.nextInt(m - i);
            int j = i + a;

            T temp = ts[i];
            ts[i] = ts[j];
            ts[j] = temp;
        }
    }
}
